package handax.GUI;

import handax.HandaxView;
import handax.controller.DBManager;
import handax.entities.Reservation;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *
 * @author Apostolidis Andreas
 */
public class RegisteredUser extends javax.swing.JFrame {

    private DBManager dbm;
    private HandaxView root;
    private String user;
    private Reservation reservation;
    private ReservSystem service;

    /** Creates new form RegisteredUser */
    public RegisteredUser() {
        dbm = new DBManager();
        initComponents();

    }

    //μέθοδος για να ορίσουμε το αρχικό παράθυρο
    public void setRoot(HandaxView root) {
        this.root = root;
    }

    //μέθοδος που παίρνει το όνομα του user που έκανε login
    public void setUser(String user) {
        this.user = user;
    }

    //μέθοδος που επιστρέφει το όνομα του user
    public String getUser() {
        return user;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        defaultUserNameStore1 = new org.jdesktop.swingx.auth.DefaultUserNameStore();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        searchRestaurantButton = new javax.swing.JButton();
        startServiceButton = new javax.swing.JButton();
        pauseServiceButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(handax.HandaxApp.class).getContext().getResourceMap(RegisteredUser.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        setName("Form"); // NOI18N

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel1.setName("jPanel1"); // NOI18N

        searchRestaurantButton.setText(resourceMap.getString("searchRestaurantButton.text")); // NOI18N
        searchRestaurantButton.setToolTipText(resourceMap.getString("searchRestaurantButton.toolTipText")); // NOI18N
        searchRestaurantButton.setName("searchRestaurantButton"); // NOI18N
        searchRestaurantButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchRestaurantButtonActionPerformed(evt);
            }
        });

        startServiceButton.setText(resourceMap.getString("startServiceButton.text")); // NOI18N
        startServiceButton.setName("startServiceButton"); // NOI18N
        startServiceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startServiceButtonActionPerformed(evt);
            }
        });

        pauseServiceButton.setText(resourceMap.getString("pauseServiceButton.text")); // NOI18N
        pauseServiceButton.setName("pauseServiceButton"); // NOI18N
        pauseServiceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pauseServiceButtonActionPerformed(evt);
            }
        });

        exitButton.setText(resourceMap.getString("exitButton.text")); // NOI18N
        exitButton.setName("exitButton"); // NOI18N
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                    .addComponent(pauseServiceButton, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                    .addComponent(startServiceButton, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                    .addComponent(searchRestaurantButton, javax.swing.GroupLayout.PREFERRED_SIZE, 185, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(searchRestaurantButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(startServiceButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pauseServiceButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(exitButton)
                .addContainerGap(277, Short.MAX_VALUE))
        );

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(jLabel2)
                .addContainerGap(165, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addComponent(jLabel2)
                .addContainerGap(297, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed

        // τερματισε την υπηρεσία
        if (service != null) {
            service.stop();
            service = null;           
        }
        //επανεργοποίησε το αρχικό παράθυρο
        root.getFrame().setVisible(true);
        //κατέστρεψε τον εαυτό σου
        dispose();
    }//GEN-LAST:event_exitButtonActionPerformed

    private void searchRestaurantButtonActionPerformed(java.awt.event.ActionEvent evt) {

        if (service == null) {

            int result = JOptionPane.showConfirmDialog(new JFrame(), "Η υπηρεσία δεν έχει ξεκινήσει ή βρίσκεται σε αδράνεια - θέλετε να ξεκινήσει;",
                    "",
                    JOptionPane.YES_NO_OPTION);
            //Αν επιλέξει ΝΑΙ
            if (result == JOptionPane.YES_OPTION) {
                startServiceButtonActionPerformed(null);

                //ανοίγει το παράθυρο της αναζήτησης                
                SearchRes sr = new SearchRes(dbm, service, reservation);
                sr.setRoot(this);
                sr.setUsername(user);
                sr.setVisible(true);
                this.setVisible(false);
            }
        } else {
            //ανοίγει το παράθυρο της αναζήτησης                
            SearchRes sr = new SearchRes(dbm, service, reservation);
            sr.setRoot(this);
            sr.setUsername(user);
            sr.setVisible(true);
            this.setVisible(false);
        }

    }

    private void startServiceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startServiceButtonActionPerformed
        //αν δεν υπάρχει καμια υπηρεσία τότε δημιουργεί μια νέα και την ξεκινάει 
        if (service == null) {
            service = new ReservSystem(dbm);
            service.start();
            //αλλίως(αν είναι σε παύση την επαναφέρει    
        } else if (service.isPaused()) {
            service.resume();
        }
    }//GEN-LAST:event_startServiceButtonActionPerformed

    private void pauseServiceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseServiceButtonActionPerformed
        //αν η υπηρεσία τρέχει τότε την κάνει παύση
        if (service != null && !service.isPaused()) {
            service.pause();
        }
    }//GEN-LAST:event_pauseServiceButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.auth.DefaultUserNameStore defaultUserNameStore1;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton pauseServiceButton;
    private javax.swing.JButton searchRestaurantButton;
    private javax.swing.JButton startServiceButton;
    // End of variables declaration//GEN-END:variables
}
